﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using Newtonsoft.Json;
using T.FAS.Runtime.DataModel.Dao;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.ServiceComponent.DAC.Service;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Base.Dao;

namespace T.FAS.Runtime.DataModel.Dao
{
    public class DataObjectIndexColumnELBDao : DaoService<DataObjectIndexColumn>
    { 
        public override string TableName => "FAS_DataObjectIndexColumn";
        public override string IDColumnName => "ID";
        public string ForeignColumn => "IndexID";
        public string MetaDataForeignColumn => "DataObjectID";

        public DataObjectIndexColumnELBDao() : base()
        {
            DBName = "TFAS";
        }

        /// <summary>
        /// 获取元数据内容
        /// </summary>
        /// <param name="metaDataId"></param>
        /// <returns></returns>
        public List<DataObjectIndexColumn> GetDataObjectIndexColumns(string indexId)
        {
            var result = new List<DataObjectIndexColumn>();
            if (string.IsNullOrEmpty(indexId))
            {
                return result;
            }
            List<object> objects = base.GetObjectsByFilter($"{ForeignColumn}='{indexId}'");

            if (objects != null)
            {
                objects.ForEach(obj => result.Add(obj as DataObjectIndexColumn));
            }
            return result;
        }

        /// <summary>
        /// 获取元数据内容
        /// </summary>
        /// <param name="metaDataId"></param>
        /// <returns></returns>
        public List<DataObjectIndexColumn> GetDataObjectIndexColumnsByMetaDataForeignKey(string doId)
        {
            var result = new List<DataObjectIndexColumn>();
            if (string.IsNullOrEmpty(doId))
            {
                return result;
            }
            List<object> objects = base.GetObjectsByFilter($"{MetaDataForeignColumn}='{doId}'");

            if (objects != null)
            {
                objects.ForEach(obj => result.Add(obj as DataObjectIndexColumn));
            }
            return result;
        }

        /// <summary>
        /// 添加DataObjectIndexColumn
        /// </summary>
        /// <param name="doIndexColumns"></param>
        public void CreateDataObjectIndexColumn(List<DataObjectIndexColumn> doIndexColumns)
        {
            if (doIndexColumns == null) return;
            try
            {
                base.Add(doIndexColumns);
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DO.CreateIndexColumnError, e.Message, e);
            }
        }

        /// <summary>
        /// 删除DataObjectIndexColumn
        /// </summary>
        /// <param name="DataObjectIndexColumns"></param>
        public void DeleteDataObjectIndexColumn(string indexID)
        {
            try
            {
                base.DB.ExecSqlStatement(string.Format(DaoUtil.DeleteSQLWithNoParam, this.TableName, this.ForeignColumn, indexID));
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DO.DeleteIndexColumnError, e.Message, e);
            }
        }

    }
}
